package com.example.saifly.mainPage.plan;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.saifly.R;

public class AlarmRemindActivity extends Activity {

    private MediaPlayer mp;
    private SharedPreferences sharedPreferences = MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_alarmremind;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //强制唤醒屏幕，固定写法
        final Window win=getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        //Android 8.0 后必须设置TYPE_APPLICATION_OVERLAY才能在其他应用和系统窗口上方显示提示窗口
        //用户可在手机设置中阻止应用显示，所以我们需要提醒用户开放权限
        win.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        mp = MediaPlayer.create(this,R.raw.dnf);
        mp.setLooping(true);
        mp.start();
        alarmDialog();
    }

    public void alarmDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        String text = sharedPreferences.getString("nextPlanContext","暂无");
        builder.setMessage(text);
        builder.setNegativeButton("关闭", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mp!=null){
            if (mp.isPlaying()){
                mp.stop();
            }
            mp.release();
        }
    }
}
